<?php

namespace App\Http\Controllers\Wechat;

use App\Models\UserInfo;
use App\Models\UserLibraryInfo;
use App\Validate\UserInfoValidate;
use App\Http\Controllers\Controller;
use App\Http\Controllers\LibApi\GshqLibController;
use App\Models\ActivityApply;
use App\Models\GoodsOrder;
use App\Models\ResourceUserReadBookDay;
use App\Models\ScoreInfo;
use App\Models\ScoreLog;
use App\Models\ScoreReset;
use App\Models\SignScore;
use App\Models\SystemInfo;
use App\Models\UserWechatInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 用户基本信息
 */
class UserInfoController extends Controller
{
    public $model = null;
    public $libraryInfoModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new UserInfo();
        $this->libraryInfoModel = new UserLibraryInfo();
        $this->validate = new UserInfoValidate();
    }



    /**
     * 获取用户积分明细
     * @param token ：用户 token
     * @param page 页数
     * @param limit 限制条数
     */
    public function scoreList()
    {
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? intval($this->request->limit) : 10;
        // $user_id = $this->request->user_info['id'];
        $account_id = $this->request->user_info['account_id'];
        $res = ScoreLog::select('id', 'score', 'type', 'type_id', 'intro', 'create_time')->where('account_id', $account_id)->orderByDesc('create_time')->paginate($limit)->toArray();

        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }

        $data = $this->disPageData($res);
        $data['score'] = UserLibraryInfo::where('id', $account_id)->value('score');

        foreach ($data['data'] as $key => $val) {
            if ($val['type_id'] === 0 || $val['type_id'] === '0') {
                $data['data'][$key]['type_icon'] = '兑';
            } elseif (empty($val['type_id'])) {
                $data['data'][$key]['type_icon'] = '其';
            } else {
                $data['data'][$key]['type_icon'] = ScoreInfo::where('id', $val['type_id'])->value('type_icon');
                $data['data'][$key]['type_icon'] =  $data['data'][$key]['type_icon'] ?  $data['data'][$key]['type_icon'] : '其';
            }
        }

        return $this->returnApi(200, "获取成功", true, $data);
    }



    /**
     * 个人中心首页
     * @param token ：用户 token 可选   有token先传token
     //* @param open_id ：用户 open_id   二选一
     */
    public function index()
    {
        $user_id = $this->request->user_info['id'];
        $default_head_img = config('other.img_addr') . 'default/default_head_img.png';

        $data['head_img'] = $default_head_img;
        $data['nickname'] = '';
        $data['system_info_number'] = 0;
        $data['is_unread_info'] = false;
        $data['score'] = 0;
        $data['qr_url'] = '';
        $data['qr_code'] = '';
        $data['is_sign'] = false;
        $data['invite_user'] = config('other.invite_user'); //是否显示邀请二维码按钮

        //   if ($user_id) {
        $user_info = UserInfo::where('id', $user_id)->first();
        $data['qr_url'] = $user_info['qr_url'];
        $data['qr_code'] = $user_info['qr_code'];
        if (!empty($user_info['wechat_id'])) {
            $wechat_info = UserWechatInfo::where('id', $user_info['wechat_id'])->first();

            $data['head_img'] = stripos($wechat_info['head_img'], 'http') !== false ? $wechat_info['head_img'] : config('img_addr') . $wechat_info['head_img'];
            $data['nickname'] = $wechat_info['nickname'];
        }
        //获取系统消息个数
        $data['system_info_number'] = SystemInfo::where('user_id', $user_id)->count();
        //是否有未读消息
        $data['is_unread_info'] = SystemInfo::where('user_id', $user_id)->where('is_look', 2)->count() ? true : false;

        //获取系统消息个数
        $is_sign = SignScore::where('user_id', $user_id)->where('date', date('Y-m-d'))->first();
        $data['is_sign'] = $is_sign ? true : false;

        //获取今日阅读，本周阅读，本年阅读，累计阅读时长，累计阅读天数
        $resourceUserReadBookDayModel = new ResourceUserReadBookDay();
        $data['read_duration_day'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'day'));
        $data['read_duration_week'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'week'));
        $data['read_duration_month'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'month'));
        $data['read_duration_year'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'year'));
        $data['access_day'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'year'));

        return $this->returnApi(200, "获取成功", true, $data);
    }
    /**
     * 获取用户阅读浏览量
     * @param token 用户guid
     */
    public function getUserReadDuration()
    {
        $user_id = $this->request->user_info['id'];
        $data = [];
        $resourceUserReadBookDayModel = new ResourceUserReadBookDay();
        $data['read_duration_day'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'day'));
        $data['read_duration_week'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'week'));
        $data['read_duration_month'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'month'));
        $data['read_duration_year'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'year'));
        $data['access_day'] = minute_to_hour($resourceUserReadBookDayModel->getUserReadDuration($user_id, null, null, 'year'));
        return $this->returnApi(200, "获取成功", true, $data);
    }


    /**
     * 修改头像 和 昵称
     * @param token 用户guid
     * @param nickname 用户昵称
     * @param head_img 用户头像
     */
    public function wechatInfoChange()
    {
        $wechat_id = $this->request->user_info['wechat_id'];
        $nickname = $this->request->nickname;
        $head_img = $this->request->head_img;
        if (empty($nickname) && empty($head_img)) {
            return $this->returnApi(201, "参数错误");
        }
        $userWechatInfo = new UserWechatInfo();
        $res = $userWechatInfo->wechatInfoChange($wechat_id, $nickname, $head_img);
        if ($res) {
            return $this->returnApi(200, "修改成功", true);
        }
        return $this->returnApi(202, "修改失败");
    }
}
